<?php
    $this->frontend_lib->setACss(array(assets('css/fotorama.css', true)));
    $this->frontend_lib->setAJs(array(assets('js/fotorama.js', true), shop_assets('js/easyresponsivetabs.js', $shop->template)));

    $startIndex = 0;
    $selected_id = 0;
    $selected_code = '';
    $query_code = $this->input->get('code', true);
    if($query_code!=false && trim($query_code)!=''){
        $selected_code = $query_code;
        foreach($productDetails as $index=>$item){
            if($item->getCode() == $selected_code){
                $startIndex = $index;
                $selected_id = $item->getId();
                break;
            }
        }
    }
    if($startIndex==0 && $selected_id==0) {
        foreach($productDetails as $index=>$item){
            if($item->getIsAvatar()==1){
                $startIndex = $index;
                $selected_id = $item->getId();
                $selected_code = $item->getCode();
                break;
            }
        }
    }
?>

<div class="content details-page">
	<div class="product-details">
		<div class="wrap need-add-after-bg">
            <ul class="product-head">
				<li>
					<a href="<?php echo shop_url($this->shop->getSlug()); ?>">Trang chủ</a>
					<span>::</span>
				</li>
				<li class="active-page">
					<a href="#"><?php echo $product->getTitle(); ?></a>
				</li>
				<div class="clear"></div>
			</ul>

			<div class="details-left">
				<div class="details-left-slider">
					<div class="fotorama" data-start-index='<?php echo $startIndex; ?>' id="product-img-slide">
                        <?php
                        $product_price_new = '';
    					$div_desc          = '';
    					foreach($productDetails as $item) {
    						if($product_price_new == '') {
    							if($selected_code!=''){
    								if($item->getCode() == $selected_code) {
    									$product_price_new = $item->getPriceNew();
    								}
    							} else {
    								if($item->getIsAvatar()==1) {
    									$product_price_new = $item->getPriceNew();
    								}
    							}
    						}
                        ?>
                        <a href="<?php echo product_upload($item->image, $product->shop_image_folder, false); ?>">
                            <img src="<?php echo product_upload($item->image, $product->shop_image_folder); ?>" data-caption='<?php echo $item->getCode(); ?>'/>
                        </a>
                        <?php
                        }
                        ?>
                    </div>

                    <div class="product-simple-detail_row mgt15">
                        <div class="fb-like" data-href="<?php echo product_url($product->slug); ?>" data-layout="box_count" data-action="like" data-show-faces="true"></div>
                        <div class="g-plusone" data-href="<?php echo product_url($product->slug); ?>" data-size="tall"></div>
                    </div>
				</div>
				<div class="details-left-info">
                    <div class="details-right-head">
    				    <form has-validate id="frm-product-detail" method="post" action="<?php echo page_url('cart/direct-push', array('ref'=>shop_url($shop->getSlug()))); ?>">
                            <input type="hidden" id="product-id" name="cart[id]" value="<?php echo $selected_id; ?>" />
                            <input type="hidden" id="product-price" name="cart[price]" value="<?php echo $product_price_new; ?>" />
                            <h1 id="product-in-set-name"><?php echo $product->getTitle(); ?></h1>
                            <div class="detail_row_value" id="product-in-set-category">
                                <a class="label label-danger w150" href="<?php echo shop_url($this->shop->getSlug().'/'.CATEGORY_SLUG.'/'.$product->shop_cate_slug); ?>">
                                    <i class="glyphicon glyphicon-tags"></i>&nbsp;
                                    <span><?php echo $product->shop_cate_title; ?></span>
                                </a>
                            </div>

                            <div class="product-simple-detail_row mgt15">
                        		<div class="detail_row_header">Sản phẩm cùng loại: </div>
                        		<div class="detail_row_value">
                        			<select id="product-temp-detail" class="form-control w100">
                       				<?php foreach($productDetails as $index=>$item) { ?>
                                        <?php
                                            $seleted_option = "";
                                            if($selected_code!=''){
                                                if($item->getCode() == $selected_code) {
                                                    $seleted_option = "selected='selected'";
                                                }
                                            } else {
                                                if($item->getIsAvatar()==1) {
                                                    $seleted_option = "selected='selected'";
                                                }
                                            }
                                        ?>
                                        <?php
                                        $option_data = array(
                                            'value'                 => $item->getId(),
                                            'data-price'            => $item->getPriceNew(),
                                            'data-price-old'        => $item->getPriceOld(),
                                            'data-price-format'     => currency_format($item->getPriceNew()),
                                            'data-price-old-format' => currency_format($item->getPriceOld()),
                                            'data-index'            => $index,
                                            'data-link'             => product_url($product->slug),
                                            'selected'              => $seleted_option
                                            );
                                        $option_string = implode(' ', array_map(function ($v, $k) { return $k . "='" . $v."'"; }, $option_data, array_keys($option_data)));
                                        ?>
                        				<option <?php echo $option_string; ?>><?php echo $item->getCode(); ?></option>
                        			<?php } ?>
                        			</select>
                        		</div>
                        	</div>
                            <div class="product-simple-detail_row">
                        		<div class="detail_row_header">Giá : </div>
                        		<div class="detail_row_value">
                                    <span id="order-temp-price" class="normal-price-style"><?php echo currency_format($product_price_new); ?></span>
                                </div>
                        	</div>
                            <div class="product-simple-detail_row">
                        		<label class="detail_row_header" for="product-qty">Số lượng : </label>
                        		<div class="detail_row_value">
                                    <input autocomplete="off"
                                        id="product-qty"
                                        name="cart[qty]"
                                        type="text"
                                        class="form-control w100 text-right"
                                        value="1"
                                        data-validate="required/numeric"
                                        data-validate-message='<?php echo $this->lang->line('error_quantity_invalid'); ?>'
                                    />
                                </div>
                        	</div>
                            <div class="product-simple-detail_row">
                        		<div class="detail_row_header">Tổng giá trị : </div>
                        		<div class="detail_row_value">
                                    <span id="order-temp-value" class="normal-price-style"><?php echo currency_format($product_price_new); ?></span>
                                </div>
                        	</div>
                            <div id="flg_message" class="alert alert-success hidden"></div>
                            <div class="product-simple-detail_row">
                        		<button class="btn btn-primary" role="button">
                                    <i class='glyphicon glyphicon-ok'></i>&nbsp;
                                    Mua ngay
                                </button>
                        		<button data-href="<?php echo page_url('cart/push'); ?>" id="btn-add-to-cart" type="button" class="btn btn-success">
                                    <i class="glyphicon glyphicon-shopping-cart"></i>&nbsp;
                                    Cho vào giỏ hàng
                                </button>
                        	</div>
                        </form>
                    </div>
				</div>
				<div class="clear">
				</div>
			</div>
			<div class="clear"></div>
		</div>

		<div class="product-reviwes">
			<div class="wrap need-add-after-bg">
				<div id="verticalTab" class="resp-vtabs">
					<ul class="resp-tabs-list">
						<li class="resp-tab-item resp-tab-active" aria-controls="tab_item-0" role="tab">Thông tin sản phẩm</li>
						<li class="resp-tab-item" aria-controls="tab_item-1" role="tab">Phương thức thanh toán</li>
						<li class="resp-tab-item" aria-controls="tab_item-2" role="tab">Bình luận</li>
					</ul>
					<div class="resp-tabs-container vertical-tabs">
						<h2 class="resp-accordion resp-tab-active" role="tab" aria-controls="tab_item-0">
							<span class="resp-arrow"></span>
							Mô tả
						</h2>
						<div class="resp-tab-content resp-tab-content-active" aria-labelledby="tab_item-0">
							<h3>Chi tiết</h3>
							<?php foreach($productDetails as $p){ ?>
                            <?php
                                $description_class = "";
                                if($selected_code!=''){
                                    if($p->getCode() != $selected_code) {
                                        $description_class = "hidden";
                                    }
                                } else {
                                    if($p->getIsAvatar()!=1) {
                                        $description_class = "hidden";
                                    }
                                }
                            ?>
                            <div class="desc <?php echo $description_class; ?> product-description description-<?php echo $p->getId(); ?>">
                                <?php echo $p->getDescription(); ?>
                            </div>
                            <?php } ?>
						</div>
						<h2 class="resp-accordion" role="tab" aria-controls="tab_item-1">
							<span class="resp-arrow"></span>
							Phương thức thanh toán
						</h2>
						<div class="resp-tab-content" aria-labelledby="tab_item-1">
							<h3>Phương thức thanh toán</h3>
							<?php echo $shop->getPaymentMethod(); ?>
						</div>
						<h2 class="resp-accordion" role="tab" aria-controls="tab_item-2">
							<span class="resp-arrow"></span>
							Product reviews
						</h2>
						<div class="resp-tab-content" aria-labelledby="tab_item-2">
							<div class="fb-comments" data-width="100%" data-href="<?php echo product_url($product->slug); ?>" data-numposts="5" data-colorscheme="light"></div>
						</div>
					</div>
				</div>
				<div class="clear"></div>
			</div>
		</div>
		<div class="clear"></div>
	</div>
</div>